package com.wohuijituan.whcsmerchant.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wohuijituan.whcsmerchant.R;
import com.wohuijituan.whcsmerchant.bean.GoodsAddListBean.DataEntity.GoodsEntity;

import java.util.List;

/**
 * 说明：
 * 作者：朱世元
 * 时间： 2016/9/3 11:01
 * 版本：V1.0
 * 修改历史：
 */
public class GoodsAddAdapter extends MyBaseAdapter<GoodsEntity> implements View.OnClickListener {
    public GoodsAddAdapter(Context ctx, List<GoodsEntity> dataList) {
        super(ctx, dataList);
        mImageLoader= ImageLoader.getInstance();
        mImageOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageForEmptyUri(R.drawable.ic_launcher)
                .showImageOnLoading(R.drawable.ic_launcher)
                .showImageOnFail(R.drawable.ic_launcher)
                .build();

    }
        //用于图片加载
        private DisplayImageOptions mImageOptions;
        private ImageLoader mImageLoader;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            convertView = View.inflate(ctx, R.layout.item_goods_add, null);
            vh = new ViewHolder(convertView);
            vh.btnGoodsAdd.setOnClickListener(this);
            convertView.setTag(vh);
        }else {
            vh = (ViewHolder) convertView.getTag();
        }
        GoodsEntity data = dataList.get(position);
        vh.tvGoodsName.setText(data.getCS_Name());
        vh.btnGoodsAdd.setTag(position);
        mImageLoader.displayImage(data.getCS_MainImg(), vh.ivGoodsPicture, mImageOptions);//给商品设值图片
        return convertView;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_goods_adds:
                int position = (int) v.getTag();
                if(myOnItemBtnClickListener != null){
                    myOnItemBtnClickListener.onClick(position,v);
                }else {
                    showToast(ctx,"没有设置myOnItemBtnClickListener监听");
                }
                break;
        }
    }
    /**
     * 设置监听,外部必须调用
     * @param myOnItemBtnClickListener
     */
    public void setMyOnItemBtnClickListener(MyOnItemBtnClickListener myOnItemBtnClickListener) {
        this.myOnItemBtnClickListener = myOnItemBtnClickListener;
    }

    private MyOnItemBtnClickListener myOnItemBtnClickListener;
    /**
     * 监听接口,用来传输被点击控件所在item的position
     */
    public interface MyOnItemBtnClickListener{
        public void onClick(int position,View view);
    }
    public class ViewHolder {
        public final ImageView ivGoodsPicture;
        public final TextView tvGoodsName;
        public final Button btnGoodsAdd;
        public final View root;

        public ViewHolder(View root) {
            ivGoodsPicture = (ImageView) root.findViewById(R.id.iv_goods_add_picture);
            tvGoodsName = (TextView) root.findViewById(R.id.tv_goods_add_name);
            btnGoodsAdd = (Button) root.findViewById(R.id.btn_goods_adds);
            this.root = root;
        }
    }
}
